package ma.projet.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import ma.projet.HibernateUtil;
import ma.projet.beans.RendezVous;

import ma.projet.interfaces.IDao;

public class RendezVousService implements IDao<RendezVous>{

	@Override
	public void create(RendezVous o) throws HibernateException {
		// TODO Auto-generated method stub
		Session session = null;
		Transaction tx = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			session.save(o);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RendezVous> load() {
		// TODO Auto-generated method stub
		Session session=null;
		List<RendezVous> liste;
	try{	
		
		
		session=HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		liste=session.createQuery("From RendezVous").list();
		session.getTransaction().commit();
		
		return liste;}
	catch(HibernateException ex)
	{
		session.getTransaction().rollback();
		System.out.println("Liste Vide");
		return null;
	}
	}

	@Override
	public void delete(RendezVous o) {
		// TODO Auto-generated method stub
		Session session = null;
		Transaction tx = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			session.delete(o);

			tx.commit();
			
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	@Override
	public void update(RendezVous o) {
		// TODO Auto-generated method stub
		Session session = null;
		Transaction tx = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			session.update(o);
			tx.commit();
			
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			
			session.close();
		}
	}

	@Override
	public RendezVous getById(int id) {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().openSession();
        session.getTransaction().begin();
        RendezVous p = (RendezVous) session.get(RendezVous.class, id);
        session.getTransaction().commit();
        session.close();
        return p;
	}
	
	@SuppressWarnings("unchecked")
	public List<Integer> listheure(Date d,String id){
		Session session=null;
		List<Integer> liste=new ArrayList<Integer>();
	try{	
		
		
		session=HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
		String s=format.format(d);
		Query q=session.createSQLQuery("select distinct heure From RendezVous where Med_id="+id+" and dater='"+s+"'");
		
		liste=q.list();
		
		session.getTransaction().commit();
		
		return liste;}
	catch(HibernateException ex)
	{
		session.getTransaction().rollback();
		System.out.println("Liste Vide");
		return null;
	}
	}

}